package com.ktjy.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class MyBatisUtil {
    static SqlSessionFactory sqlSessionFactory = null;
    static {
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        sqlSessionFactory = builder.build(resourceAsStream);
    }

    public static SqlSession createSqlSession(){
        return sqlSessionFactory.openSession(false);
    }

    public static void closeSqlSession(SqlSession session){
        if(session!=null){
            session.close();
        }
    }
}
